package com.share.pattern.singleton;

/**
 * 多线程时的单例
 * 
 * @author Johnny Tam
 *
 */
public class Singleton2 {

	private static Singleton2 instance = null;
	private static Object syncObj = new Object();

	private Singleton2() {

	}

	// 1) 加上同步锁
	// public static synchronized Singleton2 getInstance() {
	// if (instance == null) {
	// instance = new Singleton2();
	// }
	//
	// return instance;
	// }

	// 2) 双重锁定
	public static Singleton2 getInstance() {
		if (instance == null) {
			synchronized (syncObj) {// Singleton2.class
				if (instance == null) {
					instance = new Singleton2();
				}
			}

		}

		return instance;
	}
	
	

}
